package com.supply;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author : wushikai
 * <p>
 * date : 2021/8/10
 */

public class DynamicProxyHandler implements InvocationHandler {

    private Object object;

    public DynamicProxyHandler(final Object object) {
        this.object = object;
    }

    public DynamicProxyHandler() {

    }

    interface House {
        void buyHouse();
    }


    class HouseImpl implements House {

       public void buyHouse() {

           System.out.println("付钱");

       }

    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("买房前准备");
        Object result = method.invoke(object, args);
        System.out.println("买房后装修");
        return result;
    }

    public static void main(String[] args) {
        House buyHouse = new DynamicProxyHandler().new HouseImpl();
        House proxyBuyHouse = (House) Proxy.newProxyInstance(House.class.getClassLoader(), new  Class[]{House.class}, new DynamicProxyHandler(buyHouse));
        proxyBuyHouse.buyHouse();

        System.out.println(proxyBuyHouse.getClass());

    }
}
